/* ======================================================= */
/**
 * File : TrackOverlay.java Date : Feb 10, 2010 at 11:14:58 PM
 */
/* ======================================================= */
package fr.isima.gui.maps;

import java.io.File;
import java.io.FileInputStream;
import java.util.ArrayList;
import java.util.List;

import org.xmlpull.v1.XmlPullParser;

import android.app.ProgressDialog;
import android.content.ContentResolver;
import android.database.CharArrayBuffer;
import android.database.ContentObserver;
import android.database.Cursor;
import android.database.DataSetObserver;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Point;
import android.graphics.RectF;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.util.Xml;

import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapView;
import com.google.android.maps.Overlay;
import com.google.android.maps.Projection;

/* ======================================================= */
/**
 * @author daniel
 */
/* ======================================================= */
public class TrackOverlay extends Overlay
{

    private static final String TAG         = "TrackOverlay";

    // KML Parsing
    private static final String DESCRIPTION = "Description";
    private static final String DOCUMENT    = "Document";
    private static final String PLACEMARK   = "Placemark";
    private static final String NAME        = "Name";
    private static final String POINT       = "Point";
    private static final String COORDINATES = "Coordinates";
    private static final String KML         = "Kml";

    private String              kmlFile;
    private List<GeoPoint>      trackPoints;

    public TrackOverlay(String kmlFile)
    {
        this.kmlFile = kmlFile;

        trackPoints = parseKML(kmlFile);
    }

    @Override
    public void draw(Canvas canvas, MapView mapView, boolean shadow)
    {
        Projection projection = mapView.getProjection();
        
        //The brush
        Paint paint = new Paint();
        paint.setARGB(255, 255, 0, 0);
        paint.setAntiAlias(true);
        paint.setFakeBoldText(true);
        
        //radian for draw an rectangle
        int rad = 5;
        if(shadow == false)
        {
            for (GeoPoint point : trackPoints)
            {
                Point myPoint = new Point();
                projection.toPixels(point, myPoint);
                
                RectF oval = new RectF(myPoint.x-rad, myPoint.y-rad, myPoint.x+rad, myPoint.y+rad);
                
                canvas.drawOval(oval, paint);
            }
        }
    }

    private List<GeoPoint> parseKML(String path)
    {
        File kmlFile = new File(path);

        List<GeoPoint> trackPoints = new ArrayList<GeoPoint>();
        XmlPullParser parser = Xml.newPullParser();
        try
        {
            parser.setInput(new FileInputStream(kmlFile), null);
            int event = parser.next();
            for (; event != XmlPullParser.END_DOCUMENT; event = parser.next())
            {
                if (event == XmlPullParser.START_TAG)
                {
                    String name = parser.getName();
                    if (name.equalsIgnoreCase(COORDINATES))
                    {
                        String line = parser.nextText();
                        String[] values = line.split(",");
                        double latitude = Float.parseFloat(values[0]) * 1E6;
                        double longitude = Float.parseFloat(values[1]) * 1E6;
                        Log.d(TAG, "lat : " + latitude + " " + "long : "
                                + longitude);

                        GeoPoint point = new GeoPoint((int)longitude ,
                                (int)latitude);

                        trackPoints.add(point);
                        point = null;
                        values = null;
                        line = null;
                    }
                }

            }
        }
        catch (Exception e)
        {
            Log.e(TAG, "TrackOverlay error :" + e.getMessage());
            throw new RuntimeException(e);
        }

        return trackPoints;

    }

    public GeoPoint getFirst()
    {
        return trackPoints.get(0);
    }

}
